package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.SupplyExportDTO;
import net.wanho.po.Supply;
import net.wanho.service.SupplyService;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/2/5 11:38
 * @version: 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("supply")
public class SupplyController {
    // 供应商
    private final SupplyService supplyService;

    // 供应商列表
    @GetMapping("supply-list")
    public R supplyList() {
        return R.ok(supplyService.list());
    }


    // 所有供应商列表
    @GetMapping("supply-page-list")
    public R supplyList(@RequestParam(required = false, defaultValue = "1") int pageNum,
                        @RequestParam(required = false, defaultValue = "5") int pageSize,
                        Supply supply) {
        PageInfo<Supply> list = supplyService.getSupplyPage(pageNum, pageSize, supply);
        return R.ok(list);
    }

    // 检查供货商是否唯一
    @GetMapping("/supply-num-check")
    public R checksupplyNum(Integer supplyNum) {
        Supply one = supplyService.selectOneBySupplyNum(supplyNum);
        if (one != null) {
            return R.error(false);
        }
        return R.ok(true);
    }

    // 添加供货商
    @PostMapping("/supply-add")
    public R addsupply(@RequestBody Supply supply) {
        boolean save = supplyService.save(supply);
        if (save) {
            return R.ok("仓库添加成功！");
        }
        return R.error("添加失败");
    }

    // 修改供货商
    @PutMapping("/supply-update")
    public R updatesupply(@RequestBody Supply supply) {
        boolean update = supplyService.updateById(supply);
        if (update) {
            return R.ok("仓库修改成功！");
        }
        return R.error("修改失败");
    }

    // 删除供货商
    @DeleteMapping("/supply-delete/{id}")
    public R deletesupply(@PathVariable Integer id) {
        boolean delete = supplyService.removeById(id);
        if (delete) {
            return R.ok("仓库删除成功！");
        }
        return R.error("删除失败");
    }

    // 导出数据
    @GetMapping("/exportTable")
    public R exportTable(SupplyExportDTO supply) {
        List<Supply> list = supplyService.exportTable(supply);
        return R.ok("导出成功", list);
    }
}
